package com.yykj.backstage.all.service.impl;

import com.yykj.backstage.all.model.entity.AppSetting;
import com.yykj.backstage.all.model.repository.AppSettingRepository;
import com.yykj.backstage.all.service.AppSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AppSettingServiceImpl implements AppSettingService {
    @Override
    public AppSetting editAppSetting(AppSetting setting) {
        AppSetting appSetting = appSettingRepository.findById(1).orElse(null);
        if (appSetting == null) {
            appSetting = AppSetting.defaultAppSetting();
        }
        appSetting.setMaxArticleCnt(setting.getMaxArticleCnt());
        return appSettingRepository.save(appSetting);
    }

    @Override
    public AppSetting viewAppSetting() {
        AppSetting appSetting = appSettingRepository.findById(1).orElse(null);
        if (appSetting == null) {
            appSetting = AppSetting.defaultAppSetting();
        }
        return appSetting;
    }

    private AppSettingRepository appSettingRepository;

    @Autowired
    public void setAppSettingRepository(AppSettingRepository appSettingRepository) {
        this.appSettingRepository = appSettingRepository;
    }
}
